﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DLM.Web.MVC.Extensions
{
    public class AdminAuthorizeAttribute:AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (filterContext.HttpContext == null)
            {
                throw new ArgumentNullException("Http Context");
            }

            if ((!filterContext.HttpContext.User.Identity.IsAuthenticated)||(!filterContext.HttpContext.User.IsInRole("Admin")))
            {

                filterContext.Result = new HttpUnauthorizedResult();
                
            }
            base.OnAuthorization(filterContext);
        }

        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            filterContext.HttpContext.Request.Cookies.Clear();
            filterContext.HttpContext.Response.Cookies.Clear();
            filterContext.RequestContext.HttpContext.Response.Redirect("~/Management/Account/Login");
            //base.HandleUnauthorizedRequest(filterContext);
        }
    }
}